package com.pulaien.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.system.domain.*;
import com.pulaien.system.domain.dao.SysInternationalShippingDao;
import com.pulaien.system.domain.dao.SysModuleDao;
import com.pulaien.system.domain.SysOpinionBack;
import com.pulaien.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 *
 * @author pulaien
 */
@RestController
@Api(tags = "商城端首页管理")
@RequestMapping("/shop/home")
public class ShopHomeController extends BaseController {
    @Autowired
    private IConfigBannerService configBannerService;

    @Autowired
    private IConfigBannerH5Service configBannerH5Service;

    @Autowired
    private IPersonCenterService personCenterService;

    @Autowired
    private IClassRecommendService classRecommendService;

    @Autowired
    private IConfigPayAccountService configPayAccountService;


    @Autowired
    private SysInternationalShippingService sysInternationalShippingService;

    @Autowired
    private ConfigContactService configContactService;

    @Autowired
    private SysModuleService sysModuleService;

    @Autowired
    private SysOpinionBackService sysOpinionBackService;



    /**
     * 获取收费账户配置详情
     */
    @ApiOperation(value = "获取收费账户配置详情")
    @GetMapping(value = "/getPayAccount")
    public AjaxResult getDetail() {
        return success(configPayAccountService.getDetail());
    }


    /**
     * 查询轮播图管理列表
     */
    @ApiOperation(value = "轮播图列表")
    @GetMapping("/bannerList")
    public AjaxResult bannerList() {
        ConfigBanner configBanner = new ConfigBanner();
        configBanner.setBannerStatus("0");
        List<ConfigBanner> list = configBannerService.selectConfigBannerList(configBanner);
        return AjaxResult.success(list);
    }


    @ApiOperation(value = "H5轮播图列表")
    @GetMapping("/h5/bannerList")
    public AjaxResult bannerH5List() {
        ConfigBannerH5 configBannerH5 = new ConfigBannerH5();
        configBannerH5.setBannerStatus("0");
        List<ConfigBannerH5> list = configBannerH5Service.selectConfigBannerList(configBannerH5);
        return AjaxResult.success(list);
    }

    /**
     * 关键字搜索排行前6条
     */
    @ApiOperation(value = "关键字搜索排行前6条")
    @GetMapping("/searchRanking")
    public AjaxResult searchRanking(
            @RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang
    ) {
       // List<SearchRanking> list = configBannerService.searchRanking();
        List<SearchRanking> list = configBannerService.searchRankingByLang(lang);
        return AjaxResult.success(list);
    }

    /**
     * 查询分类推荐列表
     */
    @ApiOperation(value = "分类推荐列表")
    @GetMapping("/recommendList")
    public AjaxResult list(ClassRecommend classRecommend) {
        JSONArray result = classRecommendService.selectList(classRecommend);
        return AjaxResult.success(result);
    }


    /**
     * 获取站点帮助信息列表
     */
    @ApiOperation(value = "获取站点帮助信息列表")
    @GetMapping("/infoLst/{type}")
    public AjaxResult infoLst(@PathVariable String type,@RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang) {
        List<PersonCenter> personCenterList = personCenterService.getInfoListByCenterType(type);
        for (PersonCenter personCenter : personCenterList) {
            if (StringUtils.equals(lang,ConstantsUtil.LANG_EN)){
                personCenter.setContentZh(personCenter.getContentEn());
                personCenter.setTitleZh(personCenter.getTitleEn());
            }else if (StringUtils.equals(lang,ConstantsUtil.LANG_JA)){
                personCenter.setContentZh(personCenter.getContentJa());
                personCenter.setTitleZh(personCenter.getTitleJa());
            }
        }
        return AjaxResult.success(personCenterList);
    }


    /**
     * 获取站点帮助信息详情
     * @param id
     * @param lang
     * @return
     */
    @ApiOperation(value = "获取站点帮助信息详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable Long id,@RequestHeader(ConstantsUtil.HEAD_LANGUAGE) String lang){
        PersonCenter info = personCenterService.selectPersonCenterById(id);
        if (StringUtils.equals(lang,ConstantsUtil.LANG_EN)){
            info.setContentZh(info.getContentEn());
            info.setTitleZh(info.getTitleEn());
        }else if (StringUtils.equals(lang,ConstantsUtil.LANG_JA)){
            info.setContentZh(info.getContentJa());
            info.setTitleZh(info.getTitleJa());
        }
        return AjaxResult.success(info);
    }




    @ApiOperation(value = "后台获取国际物流数据")
    @GetMapping("/getIs")
    public AjaxResult getInternationalShipping() {

        List<SysModuleDao> list = sysModuleService.list();
        List<SysModuleDao> list_new  = new ArrayList<>();
        list.forEach(
                s-> {
                    QueryWrapper<SysInternationalShippingDao> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("del_flag" , 0);
                    queryWrapper.eq("module_id" , s.getId());
                    List<SysInternationalShippingDao> ss = sysInternationalShippingService.list(queryWrapper);
                    s.setDao(ss);
                    list_new.add(s);
                }
        );

        return AjaxResult.success(list_new);
    }


    @ApiOperation(value = "获取联系我们信息")
    @GetMapping("/getContact")
    public AjaxResult getContact() {
        ConfigContact byId = configContactService.getById(1);
        return AjaxResult.success(byId);
    }


    @ApiOperation(value = "意见反馈")
    @PostMapping("/oBack")
    public AjaxResult insertBack(@RequestBody SysOpinionBack dao) {
        dao.setStatus(0);
        boolean save = sysOpinionBackService.save(dao);
        return AjaxResult.success();
    }

}
